﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Sector7.Restaurant.CoreType.Enums;
using Sector7.Restaurant.Modules.SYST.Utility;
using Sector7.Restaurant.Modules.SYST.LocalService;
using Sector7.Framework.Enums;
using Sector7.Framework.Utility;
using Sector7.Framework.Core;

namespace Sector7.Restaurant.Modules.SYST.Windows
{
    /// <summary>
    /// AddMetaDataTypeWindow.xaml 的交互逻辑
    /// </summary>
    public partial class AddMetadataTypeWindow : AddModeWindowBase
    {
        public AddMetadataTypeWindow()
        {
            InitializeComponent();
        }

        public RS_MetadataType MetadataType
        {
            get { return m_entity as RS_MetadataType; }
            set { m_entity = value; }
        }

        private int Usage
        {
            get
            {
                int usage = 0;

                int tag = 0;
                foreach (CheckBox checkBox in wrpUsage.Children)
                {
                    if (checkBox.IsChecked == true)
                    {
                        tag = 1 << Convert.ToInt32(checkBox.Tag);
                        usage |= tag;
                    }
                }

                return usage;
            }
            set
            {
                int tag = 0;
                foreach (CheckBox checkBox in wrpUsage.Children)
                {
                    tag = 1 << Convert.ToInt32(checkBox.Tag);
                    if ((value & tag) == tag)
                    {
                        checkBox.IsChecked = true;
                    }
                }
            }
        }

        protected override void InitializeForCreating()
        {
            MetadataType = new RS_MetadataType();
            MetadataType.Metadatas = new List<RS_Metadata>();
        }

        protected override void InitializeForUpdating()
        {
            Usage = MetadataType.Usage;
            btnContinue.Visibility = Visibility.Hidden;
        }

        /// <summary>
        /// 添加元数据类型
        /// </summary>
        /// <returns></returns>
        protected override void AddData()
        {
            MetadataType.Usage = Usage;

            switch (m_crud)
            {
                case CrudType.Create:
                    MetadataType.TypeId = Aide.LocalClient.Invoke<int>(
                        service => service.AddMetadataType(MetadataType));
                    break;
                case CrudType.Update:
                    EntityDelta delta = ClientUtil.CreateDelta<EntityDelta>(typeof(RS_MetadataType));
                    delta.Conditions.Add("TypeId", MetadataType.TypeId);
                    delta.DeltaProperties.Add("TypeName", MetadataType.TypeName);
                    delta.DeltaProperties.Add("Usage", MetadataType.Usage);

                    Aide.LocalClient.Invoke<int>(
                        service => service.SetMetadataType(delta));
                    break;
                default:
                    break;
            }

            base.AddData();
        }
    }
}
